<?php

namespace app\model;

//use support\Model;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaSupplierExamineApprove
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_supplier_examine_approve';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all()
    {
        $pageNum = 10;
        $list = Db::table('wa_supplier_examine_approve')
            ->select('*')
            ->paginate($pageNum);

        return json_encode($list);
    }

    public static function postsCount($start_time,$end_time)
    {

        $start_time = date('Y-m-d H:i:s', $start_time);
        $end_time = date('Y-m-d H:i:s', $end_time);
        $provider_count =Db::table('wa_supplier_examine_approve')
            ->select('*')
//            ->where('post_type','product')
            ->whereBetween('created_at',[$start_time,$end_time])
            ->count(); // 用户数量
        return $provider_count;

    }



    public static function list($page, $pageNum,$whereNotIn, $purchaseOrdersn, $fields = ['*'], $condition = [], $between = []){
        $offset = ($page - 1) * $pageNum;
        // $purchaseOrdersn 为空 就是没有当前登录用户的审核
        // 数组转对象  get_object_vars($purchaseOrdersn)
        if (gettype($purchaseOrdersn) == "object"){
            $purchaseOrdersn = get_object_vars($purchaseOrdersn); // 对象转数组函数
        }elseif (gettype($purchaseOrdersn) == "array"){
            $purchaseOrdersn = $purchaseOrdersn;
        }

//        if (!$purchaseOrdersn){
//            $count = 0;
//            $list = [];
//        }else{
        if ($between) {
            $model = Db::table('wa_supplier_examine_approve')
                ->select($fields)
                ->where($condition)
                ->whereNotIn('status',$whereNotIn)
                ->whereBetween('created_at', $between);
        } else {
            $model = Db::table('wa_supplier_examine_approve')
                ->select($fields)
                ->where($condition)
                ->whereNotIn('status',$whereNotIn);
        }
        $count = $model->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        }


        if ($list){
            foreach ($list as $k){  
                // supplier_type 供应商类型： 1车辆供应商 2上牌供应商 3 国际货运代理 4保险供应商 5 国内物流
                $approve = WaSupplierExamineApproveFlow::find(
                    ['spf_no' => $k->spf_no,'status' => 0]);
                $approve = json_decode($approve);
                if ($approve) {
                    $approve_status = 0; // 待审批
                    $approve_status_name = '待审批';

                    $approve = WaSupplierExamineApproveFlow::all(
                        ['spf_no' => $k->spf_no,'status' => 1]);
                    $approve = json_decode($approve);
                    if ($approve) {
                        $approve_status = 1; // 已审批
                        $approve_status_name = '已审批';
                    }


                    $approve = WaSupplierExamineApproveFlow::all(
                        ['spf_no' => $k->spf_no, 'status' => 2]);
                    $approve = json_decode($approve);
                    if ($approve) {
                        $approve_status = 2; // 已驳回
                        $approve_status_name = '已驳回';
                    }

                    $approve = WaSupplierExamineApproveFlow::all(
                        ['spf_no' => $k->spf_no, 'status' => 3]);
                    $approve = json_decode($approve);
                    if ($approve) {
                        $approve_status = 3; // 已撤回
                        $approve_status_name = '已撤回';
                    }


                } else {

                    $approve_status = 999;// 待提交审批
                    $approve_status_name = '待提交审批';

                }
                

                $k->approve_status = $approve_status; // 审批状态
                $k->approve_status_name = $approve_status_name;// 审批状态名称



                $k->approve_flow = json_decode($k->approve_flow);
                // 销售单审批状态 0 审批中 1 通过 2 驳回 3作废 4删除
                if ($k->status == 0){
                    $k->status_name = '审批中';
                }elseif ($k->status == 1){
                    $k->status_name = '审批通过';
                }elseif ($k->status == 2){
                    $k->status_name = '审批驳回';
                }elseif ($k->status == 3){
                    $k->status_name = '撤回审批';
                }elseif ($k->status == 4) {
                    $k->status_name = '已删除';
                }elseif ($k->status == 5) {
                    $k->status_name = '已撤回';
                }else{
                    $k->status_name = '';
                }

            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }


    /**
     * 供应商审批 -创建
     * @param $data
     * @return int
     */
    public static function create($data)
    {
        return Db::table('wa_supplier_examine_approve')->insertGetId($data);
    }


    /**
     * 供应商审批-状态修改
     * @param $data
     * @return int
     */
    public static function updated($condition,$data)
    {
        return Db::table('wa_supplier_examine_approve')->where($condition)->update($data);
    }

    /**
     * 供应商审批-查询
     * @param $data
     * @return int
     */
    public static function find($condition,$adminId)
    {
        $res = Db::table('wa_supplier_examine_approve')
            ->where($condition)
            ->first();
        if ($res){
            $where_field = ['*'];
            $where_find = [];
            $where_find [] = ['id', '=', $res->supplier_id];
            // 获取的 对应的审批编号
            // 供应商类型： 1车辆供应商 2上牌供应商 3 国际货运代理 4保险供应商 5 国内物流
            // 审批状态 0 待审批 1 审批通过 2 审批驳回
            if ($res->supplier_type == 1){
                $supplier = WaSupplier::find($where_find);
                $supplier = json_decode($supplier);
            }elseif ($res->supplier_type == 2){
                $supplier = WaSupplierRegistrationFee::find($where_find);
                $supplier = json_decode($supplier);
            }elseif ($res->supplier_type == 3){
                $supplier = WaSupplierTransferExportFee::find($where_find);
                $supplier = json_decode($supplier);
            }elseif ($res->supplier_type == 4){
                $supplier = WaSupplierInsuranceCompany::find($where_find);
                $supplier = json_decode($supplier);
            }elseif ($res->supplier_type == 5){
                $supplier = WaSupplierDomesticLogisticsFees::find($where_find);
                $supplier = json_decode($supplier);
            }
            if ($supplier){
                if ($res->spf_no){
                    $where_approve_flow = [];
                    $where_approve_flow [] = ['spf_no', '=', $res->spf_no];
                    $where_approve_flow [] = ['status', '=', 3];
                    $approve = WaSupplierExamineApproveFlow::all($where_approve_flow);
                    $approve = json_decode($approve);
                    if ($approve){
                        $approve_status = 3; // 已撤回
                        $approve_status_name = '已撤回';

                    }else{
                        $where_approve_flow [] = ['status', '=', 2];
                        $approve = WaSupplierExamineApproveFlow::all($where_approve_flow);
                        $approve = json_decode($approve);
                        if ($approve){
                            $approve_status = 2; // 已驳回
                            $approve_status_name = '已驳回';
                        }else{
                            $where_approve_flow [] = ['status', '=', 1];
                            $approve = WaSupplierExamineApproveFlow::all($where_approve_flow);
                            $approve = json_decode($approve);
                            if ($approve){
                                $approve_status = 1; // 已审批
                                $approve_status_name = '已审批';
                            }else{
                                $where_approve_flow [] = ['status', '=', 0];
                                $approve = WaSupplierExamineApproveFlow::all($where_approve_flow);
                                $approve = json_decode($approve);
                                if ($approve){
                                    $approve_status = 0; // 待审批
                                    $approve_status_name = '待审批';
                                }else{
                                    $approve_status = 999;// 待提交审批
                                    $approve_status_name = '待提交审批';
                                }
                            }
                        }

                    }
                    $res->approve_status = $approve_status; // 审批状态
                    $res->approve_status_name = $approve_status_name;// 审批状态名称
                }
            }

        }

        return json_encode($res);
    }


}